package com.flycent.hqhs.ui;


import java.lang.ref.WeakReference;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flycent.hqhs.App;
import com.flycent.hqhs.R;
import com.flycent.hqhs.bean.VersionInfo;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.http.WebException;
import com.flycent.hqhs.ui.view.DoubleButtonDialog;
import com.flycent.hqhs.util.ContextUtil;
import com.flycent.hqhs.util.FileUtil;
import com.flycent.hqhs.util.PackageInfoUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 设置
 * @author fbl
 */
public class SettingActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.setting_relat1 , click = "butClick") RelativeLayout mVersionRelat;   //检查更新
	@ViewInject(id = R.id.setting_relat2 , click = "butClick") RelativeLayout mClearRelat;   //清除缓存
	@ViewInject(id = R.id.modify_password_relat , click = "butClick") RelativeLayout mModifyPassRelat;   //
	@ViewInject(id = R.id.setting_about_relat , click = "butClick") RelativeLayout mAboutRelat;     //关于我们
	@ViewInject(id = R.id.setting_help_relat , click = "butClick") RelativeLayout mHelpRelat;         //帮助中心
	@ViewInject(id = R.id.setting_feedback_relat , click = "butClick") RelativeLayout mFeedBackRelat; //反馈
	@ViewInject(id = R.id.setting_exit_but , click = "butClick") Button mExitBut;                      //退出
	@ViewInject(id = R.id.setting_version_text ) TextView mSettingVersionText;                    //退出

	private DoubleButtonDialog mDoubleDialog;

	public static SettingActivity instance = null;
	private Handler mHandler = new MyHandler(this);
	private int mVersionCode  ;
	private VersionInfo mVersion;

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_setting);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle(R.string.title_setting);
		instance = this;
		//apk当前版本
		mSettingVersionText.setText("当前版本 V "+PackageInfoUtil.getVersionName(App.getInstance()));
		mVersionCode = PackageInfoUtil.getVersionCode(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		JudgeLogin();
	}


	public void butClick(View v){
		//
		if( v == mVersionRelat){
			doCheckVersion(); //检测版本

		}
		//
		if( v == mClearRelat ){
			doDeleteCache(); // 删除图片缓存
		}
		//
		if( v == mModifyPassRelat){
			ContextUtil.startActivity(this, ModifyPwdActivity.class);
		}

		//关于我们
		if( v == mAboutRelat ){
			ContextUtil.startActivity(this, AboutActivity.class);
		}
		//帮助中心
		if( v == mHelpRelat ){
			ContextUtil.startActivity(this, HelpActivity.class);
			//			ContextUtil.startActivity(this, ProductEvaluationActivity.class);
		}
		//反馈
		if( v == mFeedBackRelat ){
			ContextUtil.startActivity(this, FeedBackActivity.class);
		}
		//退出登录
		if( v == mExitBut ){
			showExitDialog();
		}
	}

	/** 检测版本 */
	private void doCheckVersion() {
		showLoadingDialog("");
		
		
		new Thread(){
			public void run() {
				try {
					mVersion = WebClient.getInstance(SettingActivity.this).getVersion();
					mHandler.sendEmptyMessage(1);
				} catch(Exception e){
					mHandler.sendEmptyMessage(4);
				}
			};
		}.start();
		
		
	}


	/** 删除图片缓存*/
	private void doDeleteCache() {
		showLoadingDialog("");
		ImageLoader.getInstance().clearDiscCache();//sd卡缓存
		ImageLoader.getInstance().clearMemoryCache(); //内存的缓存
		FileUtil.clearFile(FileUtil.SDCARDPATH);  //清空内存卡中的内容

		mHandler.sendEmptyMessageDelayed(0, 1*1000);
	}

	/** 显示退出登录框 */
	private void showExitDialog() {
		dismissDeleteDialog();
		mDoubleDialog = new DoubleButtonDialog(this);
		mDoubleDialog.showDialog(getString(R.string.info_is_exit), new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				new Thread(){
					public void run() {
						try {
							WebClient.getInstance(SettingActivity.this).logout();
							mHandler.sendEmptyMessage(2);
						} catch (WebException e) {

						}	
					};
				}.start();

			}
		});

	}
	/** 取消dialog */
	private void dismissDeleteDialog() {

		if( mDoubleDialog != null && mDoubleDialog.isShowing() ) {
			mDoubleDialog.dismiss();
		}
		mDoubleDialog = null;

	}
	//判断是否处于登录状态
	private void JudgeLogin(){
		new Thread(){
			public void run() {
				try {
					WebClient.getInstance(SettingActivity.this).checkLogin();
					mHandler.sendEmptyMessage(3);
				} catch (WebException e) {
					if( "shop.common.unauthorized".equals(e.getStatusCode()) ){
						mHandler.sendEmptyMessage(2);
						return;
					}else {
						e.printStackTrace();
					}

				}catch (Exception e) {
				}
			};
		}.start();
	}

	@SuppressLint("HandlerLeak") 
	private class MyHandler extends Handler{
		private final WeakReference<Activity> mActivity;

		public MyHandler(Activity activity) {  
			mActivity = new WeakReference<Activity>(activity);  
		}  
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);

			if( mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					dismissLoadingDialog();
					App.showShortToast(R.string.cache_is_delete);
					break;
				case 1:
					dismissLoadingDialog();
					
					if(mVersionCode < mVersion.getVersionCode()   ){
						App.showShortToast("需要升级");
						//处理升级操作
					}else {
						App.showShortToast(R.string.info_app_connot_update);
					}
					break;
				case 2:
					mExitBut.setVisibility(View.GONE);
					break;
				case 3:
					mExitBut.setVisibility(View.VISIBLE);
					break;
				case 4:
					dismissLoadingDialog();
					App.showShortToast(R.string.request_failure);
					
					break;

				default:
					break;
				}
			}
		}
	}

	@Override
	protected void onDestroy() {
		dismissDeleteDialog();
		super.onDestroy();
		mHandler.removeCallbacksAndMessages(null);
	}

}
